﻿namespace WebSilverlight.Views
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ServiceModel.DomainServices.Client;
    using System.Windows;
    using System.Windows.Controls;
    using WebSilverlight.Web;

    public partial class ClienteEditWindow : ChildWindow
    {
        #region Properties
            private IList<OperationBase> possiblyPendingOperations = new List<OperationBase>();


            private ClienteWrapper _Current;
            public ClienteWrapper Current {
                get { return _Current; }
                set { 
                    this._Current = value;
                    this.Edit1.Current = value;
                    this.Edit2.Current = value;
                }
            }
            public ClienteWrapper Original { get; set; }
            public bool IsNew { get; set; }
            public bool IsEditingFirstState { get; set; }
            public ClienteDomainContext DomainContext { get; set; }
            public DomainDataSource DDS { get; set; }

        #endregion

        public ClienteEditWindow()
        {
            WindowInit(false);
        }
        public ClienteEditWindow(bool IsEditing)
        {
            WindowInit(IsEditing);
        }

        private void WindowInit(bool IsEditing)
        {
            InitializeComponent();

            IsEditingFirstState = IsEditing;
            IsNew = false;            

            this.Edit1.ParentWindow = this;
            this.Edit2.ParentWindow = this;

            this.LayoutUpdated += this.GoToInitialState;             
        }
        
        private void GoToInitialState(object sender, EventArgs eventArgs)
        {
            if (IsEditingFirstState)
                NavigateToEdit();
            else
                NavigateToView();
            this.LayoutUpdated -= this.GoToInitialState;
        }  
        public virtual void NavigateToEdit()
        {
            this.Title = "Edit";
            VisualStateManager.GoToState(this, "AtFirstChild", true);
        }
        public virtual void NavigateToView()
        {
            this.Title = "Cliente";	
            VisualStateManager.GoToState(this, "AtSecondChild", true);
        }
        public virtual void NavigateToEnd()
        {   
            Close();
        }    
        private void Window_Closing(object sender, CancelEventArgs eventArgs)
        {            
            foreach (OperationBase operation in this.possiblyPendingOperations)
            {
                if (!operation.IsComplete)
                {
                    if (operation.CanCancel)
                    {
                        operation.Cancel();
                    }
                    else
                    {
                        eventArgs.Cancel = true;
                    }
                }
            }
        }
        public void AddPendingOperation(OperationBase operation)
        {
            this.possiblyPendingOperations.Add(operation);
        }
    }
}